<?php

/**
 * @copyright Copyright(c) 2016
 * @brief 模型表业务处理
 * @author jumark
 * @date 2016-8-16
 * @version 1.0
 * @note
 */
/**
 * @brief Model
 * @class Model
 * @note
 */

namespace Operation\Model;
use Think\IFilter;

class Model {

    /**
     * @brief 商品模型添加/修改
     * @param string $model_id 		模型编号
     * @param string $model_name 	模型名字
     * @param array $attribute 		表字段 数组格式,如Array ([name] 	=> Array ( [0] => '' )
     * 								 [show_type] => Array ( [0] => '' )
     * 								 [value] 	=> Array ( [0] => '' )
     * 								 [is_seach] 	=> Array ( [0] => 1 ))
     * @param array $spec 			表字段 数组格式,如Array ([id] 	=> Array ( [0] => '' ))
     * @return bool bool:true成功；false失败
     */
    public function model_update($model_id, $model_name, $attribute, $spec) {
        if (!$model_name) {
            return false;
        }

        //初始化model商品模型表类对象
        $modelObj = M('model');

        //设置更新字段数据
        $dataArray = array(
            'name' => $model_name
        );

        if ($model_id) {
            //更新商品模型数据
            $dataArray['id'] = $model_id;
            $modelObj->save($dataArray);
        } else {
            //添加新商品模型
            $model_id = $modelObj->add($dataArray);
        }

        if ($model_id) {
            //新增、更新模型扩展属性
            if ($attribute) {
                $this->attribute_update($attribute, $model_id);
            }

            //有规格数据
            if ($spec) {
                //新增、更新模型规格
                $model_spec = array();
                $spec_len = count($spec['id']);
                for ($i = 0; $i < $spec_len; $i++) {
                    $model_spec[] = $spec['id'][$i];
                }
                
                //更新商品模型规格
                $modelObj->save(array('spec_ids' => join(',', $model_spec), 'id' => $model_id));
            }

            return true;
        } else {
            return false;
        }
    }

    /**
     * @brief 商品属性添加/修改
     * @param array $attribute 表字段 数组格式,如Array ([name] 		=> Array ( [0] => '' )
     * 							 [show_type] => Array ( [0] => '' )
     * 							 [value] 	=> Array ( [0] => '' )
     * 							 [is_seach] 	=> Array ( [0] => 1 ))
     * @param int $model_id 模型编号
     */
    public function attribute_update($attribute, $model_id) {
        //初始化attribute商品模型属性表类对象
        $attributeObj = M('attribute');
        $len = count($attribute['name']);
        $ids = "";
        for ($i = 0; $i < $len; $i++) {
            if (isset($attribute['name'][$i]) && isset($attribute['value'][$i])) {
                $options = str_replace('，', ',', $attribute['value'][$i]);
                $type = isset($attribute['is_search'][$i]) ? $attribute['is_search'][$i] : 0;

                //设置商品模型扩展属性 字段赋值
                $filedData = array(
                    "model_id" => intval($model_id),
                    "type" => IFilter::act($attribute['show_type'][$i]),
                    "name" => $attribute['name'][$i],
                    "value" => rtrim($options, ','),
                    "search" => IFilter::act($type)
                );

                $id = intval($attribute['id'][$i]);
                if ($id) {
                    //更新商品模型扩展属性
                    $filedData['id'] = $id;
                    $attributeObj->save($filedData);
                } else {
                    //新增商品模型扩展属性
                    $id = $attributeObj->add($filedData);
                }
                $ids .= $id . ',';
            }
        }

        if ($ids) {
            $ids = trim($ids, ',');

            //删除商品模型扩展属性
            $where = array(
                'model_id' => $model_id,
                'id not in (' . $ids .')'
            );
            $attributeObj->delete($where);
        }
    }

    /**
     * @brief 将$attribute、$spec的POST数组转换成正常数组
     * @param array $attribute 表字段 数组格式,如Array ([name] 		=> Array ( [0] => '' )
     * 							 [show_type] => Array ( [0] => '' )
     * 							 [value] 	=> Array ( [0] => '' )
     * 							 [is_seach] 	=> Array ( [0] => 1 ))
     * @param array $spec 		表字段 数组格式,如Array ([id] 	=> Array ( [0] => '' ))
     * @return array
     */
    public function postArrayChange($attribute, $spec) {
        $len = count($attribute['name']);
        $model_attr = array();
        for ($i = 0; $i < $len; $i++) {
            $model_attr[$i]['id'] = $attribute['id'][$i];
            $model_attr[$i]['name'] = $attribute['name'][$i];
            $model_attr[$i]['type'] = $attribute['show_type'][$i];
            $model_attr[$i]['value'] = $attribute['value'][$i];
            $model_attr[$i]['search'] = $attribute['is_search'][$i];
        }
        $spec_len = count($spec['name']);
        $model_spec = array();
        for ($i = 0; $i < $len; $i++) {
            $model_spec[$i]['id'] = $spec['id'][$i];
        }
        return array('model_attr' => $model_attr, 'model_spec' => $model_spec);
    }

    /**
     * @brief 根据模型编号  获取模型详细信息
     *
     * @param int $model_id 模型编号
     *
     * @return array 数组格式 	Array ( [id] => '',[name] => '', [model_attr] => Array ( ),[model_spec] => Array ( ))
     */
    public function get_model_info($model_id) {
        $model_id = intval($model_id);
        //初始化model商品模型表类对象
        $modelObj = M('model');
        //根据模型编号  获取商品模型详细信息
        $model_info = $modelObj->find($model_id);
        if ($model_info) {
            //反序列化 商品模型规格数据
            $model_info['model_spec'] = array();
            if ($model_info['spec_ids']) {
                $specDB = M('spec');
                $model_info['model_spec'] = $specDB->where("id in (" . $model_info['spec_ids'] . ")")->select();
            }

            //初始化attribute商品模型属性表类对象
            $attributeObj = M('attribute');
            //根据商品模型编号 获取商品模型扩展属性
            $model_attr = $attributeObj->where("model_id = " . $model_id)->select();
            $model_info['model_attr'] = $model_attr;
        }
        return $model_info;
    }

}